package org.jdsm.flow.core.users;

import org.jdsm.flow.core.users.dao.IUserDao;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * User: mikom
 * Date: 11/27/12
 * Time: 5:34 PM
 */
public class UserService implements IUserService {


    @Resource
    private IUserDao userDAO;

    @Override
    public IUser loadUser(IUser user) {
        return userDAO.loadUser(user);
    }

    @Override
    public Set<IRole> findUserRoles(IUser user) {
        return new HashSet<IRole>(userDAO.findUserRolesByUserId(user.getId()));
    }

    @Override
    public Set<IGroup> findUserGroups(IUser user) {
        return new HashSet<IGroup>(userDAO.findUserGroupsByUserId(user.getId()));
    }

    @Override
    public IRole loadRole(IRole role) {
        return userDAO.loadRole(role);
    }

    @Override
    public Set<IUser> findUsersForRole(IRole role) {
        return null;
    }

    @Override
    public Set<IRole> findAllowedRoles(IRole role) {
        return null;
    }

    @Override
    public IGroup loadGroup(IGroup group) {
        return userDAO.loadGroup(group);
    }

    @Override
    public Set<IUser> findUsersForGroup(IGroup group) {
        return null;
    }

    @Override
    public Set<IGroup> findAllowedGroups(IGroup group) {
        return null;
    }

    @Override
    public Set<IUser> loadUsersByCriteria(int maxResult, int startResult, boolean isSort, String field) {
        return new HashSet<IUser>(userDAO.loadUsersByCriteria(maxResult, startResult, isSort, field));
    }

    @Override
    public Set<IGroup> loadGroupsByCriteria(int maxResult, int startResult, boolean isSort, String field) {
        return new HashSet<IGroup>(userDAO.loadGroupsByCriteria(maxResult, startResult, isSort, field));
    }

    @Override
    public Set<IRole> loadRolesByCriteria(int maxResult, int startResult, boolean isSort, String field) {
        return new HashSet<IRole>(userDAO.loadRolesByCriteria(maxResult, startResult, isSort, field));
    }

    @Override
    public void removeUser(IUser user) {
        userDAO.removeUser(user.getId());
    }

    @Override
    public void removeRole(IRole role) {
        userDAO.removeRole(role.getId());
    }

    @Override
    public void removeGroup(IGroup group) {
        userDAO.removeGroup(group.getId());
    }

    @Override
    public void includeUser(IUser user, IRole role) {
        Collection<IRole> roles= new ArrayList<IRole>(1);
        roles.add(role);
        userDAO.enableUserToRoles(user, roles);
    }

    @Override
    public void includeUser(IUser user, IGroup group) {
        Collection<IGroup> groups = new ArrayList<IGroup>(1);
        groups.add(group);
        userDAO.enableUserToGroups(user, groups);
    }

    @Override
    public void includeUserToRoles(IUser user, Set<IRole> roles) {
        userDAO.enableUserToRoles(user, roles);
    }

    @Override
    public void includeUserToGroups(IUser user, Set<IGroup> groups) {
        userDAO.enableUserToGroups(user, groups);
    }

    @Override
    public void excludeUser(IUser user, IRole role) {

    }

    @Override
    public void excludeUser(IUser user, IGroup group) {

    }

    @Override
    public void excludeUserFromRoles(IUser user, Set<IRole> roles) {

    }

    @Override
    public void excludeUserFromGroups(IUser user, Set<IGroup> groups) {

    }

    @Override
    public IUser saveOrUpdate(IUser user) {
        return userDAO.saveOrUpdate(user);
    }

    @Override
    public IRole saveOrUpdate(IRole role) {
        return userDAO.saveOrUpdate(role);
    }

    @Override
    public IGroup saveOrUpdate(IGroup group) {
        return userDAO.saveOrUpdate(group);
    }

    @Override
    public Set<IGroup> loadAllowedGroupsByPermission(Long permission) {
        return null;
    }

    @Override
    public Set<IRole> loadAllowedRolesByPermission(Long permission) {
        return null;
    }

    @Override
    public boolean isAllowedUserAccess(IUser user, IGroup group) {
        return false;
    }

    @Override
    public boolean isAllowedUserAccess(IUser user, IRole role) {
        return false;
    }

    @Override
    public IUser findUserByName(String name) {
        return userDAO.findUserByName(name);
    }

    @Override
    public IUser findUserByNickName(String nickName) {
        return userDAO.findUserByNickName(nickName);
    }

    @Override
    public IRole findRoleByName(String name) {
        return userDAO.findRoleByName(name);
    }

    @Override
    public IGroup findGroupByName(String name) {
        return userDAO.findGroupByName(name);
    }

    @Override
    public Set<IRole> findRolesByPermission(Long permission) {
        return null;
    }

    @Override
    public Set<IGroup> findGroupsByPermission(Long permission) {
        return null;
    }

    @Override
    public void reassignUserRoles(IUser user, Set<IRole> roles) {

        userDAO.reassignUserRoles(user, roles);
    }

    @Override
    public void reassignUserGroups(IUser user, Set<IGroup> groups) {
        userDAO.reassignUserGroups(user, groups);
    }

    @Override
    public int usersCount() {
        return userDAO.usersCount();
    }

    @Override
    public int rolesCount() {
        return userDAO.rolesCount();
    }

    @Override
    public int groupCount() {
        return userDAO.groupCount();
    }

    @Override
    public boolean hasUserAccessToRolesByName(IUser user, Set<String> rolesName) {
        return false;
    }

    @Override
    public boolean hasUserAccessToRoles(IUser user, Set<IRole> roles) {
        return false;
    }

    @Override
    public boolean hasUserAccessToGroupByName(IUser user, Set<String> groupsName) {
        return false;
    }

    @Override
    public boolean hasUserAccessToGroups(IUser user, Set<IGroup> groups) {
        return false;
    }

    @Override
    public IUser newUserInstance() {
        return userDAO.newUserInstance();
    }

    @Override
    public IGroup newGroupInstance() {
        return userDAO.newGroupInstance();
    }

    @Override
    public IRole newRoleInstance() {
        return userDAO.newRoleInstance();
    }

    @Override
    public IRole newRoleInstance(String roleName, Long permission) {
        return userDAO.newRoleInstance(roleName, permission);
    }

    @Override
    public IGroup newGroupInstance(String groupName, Long permission) {
        return userDAO.newGroupInstance(groupName, permission);
    }

    @Override
    public boolean hasUserAccess(IUser user, Set<String> roleNames, Set<String> groupsName) {
        return false;
    }

    @Override
    public void setUserName(IUser user, String newUserName) {
        userDAO.setUserName(user, newUserName);
    }

    @Override
    public void setPassword(IUser user, String password) {
        userDAO.setPassword(user, password);
    }

    @Override
    public void setHint(IUser user, String hint) {

    }

    @Override
    public void setNickName(IUser user, String nickName) {

    }
}
